﻿Public Class Form1
    '圆周率计算器，根据pi的莱布尼茨公式制作。By hhh
    Dim n As Decimal
    Dim m As Decimal
    Dim o As Integer

    Dim mdbf As Decimal 'm devided by four，m除以4
    Dim mdb2 As Decimal 'm devided by 2，m除以2
    Dim sfzsm As Decimal '四分之三m

    Dim pidbf As Double 'pi devided by four，pi除以4
    Dim pi As Double

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


        m = TextBox1.Text '赋值，m为文本框里的数值

        If m < 7.9E+27 And m > 1 Then


            o = 1 '根据公式，应该是一次减一次加，o为判断是减还是加
            mdbf = m / 4
            mdb2 = m / 2
            sfzsm = （3 / 4) * m
            pidbf = 1

            If Not m > 100000000.0 Then
                ProgressBar1.Value = 5

                For n = 1 To m Step 2

                    '///////进度条1/////////
                    'If n < mdbf Then
                    '    ProgressBar1.Value = 0
                    'Else
                    '    If n >= mdbf And n < mdb2 Then
                    '        ProgressBar1.Value = 25
                    '    Else
                    '        If n >= mdb2 And n < sfzsm Then
                    '            ProgressBar1.Value = 50
                    '        Else
                    '            ProgressBar1.Value = 75
                    '        End If
                    '    End If
                    'End If
                    '///////进度条1/////////

                    '///////进度条2/////////
                    If n >= mdbf And n < mdb2 Then ProgressBar1.Value = 25
                    If n >= mdb2 And n < sfzsm Then ProgressBar1.Value = 50
                    If n >= sfzsm Then ProgressBar1.Value = 75
                    '///////进度条2/////////

                    '////////计算/////////
                    If o = 1 Then
                        pidbf = pidbf - 1 / (n + 2)
                        o = 2
                    Else
                        pidbf = pidbf + 1 / (n + 2)
                        o = 1
                    End If

                Next

                pi = 4 * pidbf
                '////////计算/////////


                ProgressBar1.Value = 100

                Pioutput() '输出

                MsgBox("计算完成，已导出结果于软件根目录下的result文件夹中")

                ProgressBar1.Value = 0
            Else

                For n = 1 To m Step 2

                    If o = 1 Then
                        pidbf = pidbf - (1 / (n + 2))
                        o = 2
                    Else
                        pidbf = pidbf + (1 / (n + 2))
                        o = 1
                    End If

                Next

                pi = 4 * pidbf

                ProgressBar1.Value = 100

                Pioutput() '输出

                MsgBox("计算完成，已导出结果于软件根目录下的result文件夹中")

                ProgressBar1.Value = 0

            End If


        Else
            MsgBox("输入值必须小于7.9E+27，且大于1")

        End If

    End Sub

    Function Pioutput()
        FileOpen(1, "result\result.txt", OpenMode.Output)
        Print(1, pi)
        FileClose(1)
    End Function

End Class
